/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.urchr.todoist.lib;

/**
 *
 * @author Kostya
 */
public class RichNameParser {
    private String clearName = "";
    private String origName = "";
    private boolean asterisk = false;
    
    public static String replaceAll(String s, String src, String dst){
        String res = "";
        while(s.indexOf(src)!=-1){
            res = res+s.substring(0, s.indexOf(src))+dst;
            s = s.substring(s.indexOf(src)+src.length());
        }
        res = res + s;
        return res;
    }
    
    public String parseData(String data){
        setOrigName(data);
        setClearName(getOrigName().trim());
        setAsterisk(false);
        if(getClearName().startsWith("*")){
            setAsterisk(true);
            setClearName(getClearName().substring(1).trim());
        }
        while(getClearName().indexOf("%(")!=-1){
            int stIndex = getClearName().indexOf("%(");
            int endIndex = getClearName().indexOf(")", stIndex);
            int closingIndex = getClearName().indexOf("%", endIndex);
            if(stIndex==-1 || endIndex==-1 || closingIndex==-1)
                break;
            setClearName(getClearName().substring(0, stIndex)+getClearName().substring(endIndex+1));
            closingIndex = getClearName().indexOf("%");
            setClearName(getClearName().substring(0, closingIndex)+getClearName().substring(closingIndex+1));
        }
        clearName = replaceAll(clearName, "\\n", "\n");
        return getClearName();
    }

    public String getClearName() {
        return clearName;
    }

    public void setClearName(String clearName) {
        this.clearName = clearName;
    }

    public String getOrigName() {
        return origName;
    }

    public void setOrigName(String origName) {
        this.origName = origName;
    }

    public boolean isAsterisk() {
        return asterisk;
    }

    public void setAsterisk(boolean asterisk) {
        this.asterisk = asterisk;
    }
}
